//
//  RDStatusBar.m
//  eCourseApp
//
//  Created by 飞猪 on 2018/9/2.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RDStatusBar.h"

@implementation RDStatusBar

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    _totalPageCounts = 1;
    _curPageIndex = 1;
    
    [self addSubview:self.label];
  }
  return self;
}

- (void)layoutSubviews {
  [super layoutSubviews];
  
  [self.label sizeToFit];
  
  if ([self isFringeScreen]) {
    self.label.origin = CGPointMake(0, self.bounds.size.height - self.label.bounds.size.height);
  }
  else {
    self.label.origin = CGPointMake(0, (self.bounds.size.height - self.label.bounds.size.height) * 0.5);
  }
  
}

- (BOOL)isFringeScreen {
  if(@available(iOS 11.0, *)) {
    return !UIEdgeInsetsEqualToEdgeInsets([UIApplication sharedApplication].windows.firstObject.safeAreaInsets, UIEdgeInsetsZero);
  }
  else {
    return NO;
  }
}

- (void)setTotalPageCounts:(NSInteger)totalPageCounts {
  _totalPageCounts = totalPageCounts;

  NSString *text = [NSString stringWithFormat:@"%@/%@", [@(self.curPageIndex + 1) stringValue], [@(_totalPageCounts) stringValue]];
  self.label.text = text;
}

- (void)setCurPageIndex:(NSInteger)curPageIndex {
  _curPageIndex = curPageIndex;
  
  NSString *text = [NSString stringWithFormat:@"%@/%@", [@(_curPageIndex + 1) stringValue], [@(_totalPageCounts) stringValue]];
  self.label.text = text;
}

- (UILabel *)label {
  if (!_label) {
    _label = [[UILabel alloc] init];
    _label.textAlignment = NSTextAlignmentCenter;
    _label.textColor = [UIColor blackColor];
    _label.font = [UIFont systemFontOfSize:11.f];
  }
  return _label;
}

@end
